package com.pshcp.service;

import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.PatientUnpayFeeRecordDTO;
import com.pshcp.domain.model.PatientUnpayFeeRecord;
import com.pshcp.mapper.PatientUnpayFeeRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 患者诊间费用支付记录表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2023-02-13
 */
@Service
public class PatientUnpayFeeRecordService {

    @Autowired
    private PatientUnpayFeeRecordMapper patientUnpayFeeRecordMapper;

    public PageList<PatientUnpayFeeRecord> list(PatientUnpayFeeRecordDTO patientUnpayFeeRecordDTO){
        if(patientUnpayFeeRecordDTO.getPageNum() != null && patientUnpayFeeRecordDTO.getPageSize() != null){
            PageHelper.startPage(patientUnpayFeeRecordDTO.getPageNum(), patientUnpayFeeRecordDTO.getPageSize());
        }
        Example example = new Example(PatientUnpayFeeRecord.class);
        Example.Criteria criteria = example.createCriteria();
        if(patientUnpayFeeRecordDTO.getStatus() != null){
            criteria.andEqualTo("status", patientUnpayFeeRecordDTO.getStatus());
        }else{
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        example.setOrderByClause(" created_time desc");
        List<PatientUnpayFeeRecord> list = patientUnpayFeeRecordMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<PatientUnpayFeeRecord> select(PatientUnpayFeeRecord patientUnpayFeeRecord){
        return patientUnpayFeeRecordMapper.select(patientUnpayFeeRecord);
    }

    public int insert(PatientUnpayFeeRecord patientUnpayFeeRecord){
        patientUnpayFeeRecord.setId(SystemUtil.getId());
        patientUnpayFeeRecord.setStatus(CommonStatusEnum.INIT.getCode());
        patientUnpayFeeRecord.setCreatedTime(new Date());
        return patientUnpayFeeRecordMapper.insert(patientUnpayFeeRecord);
    }

    public int update(PatientUnpayFeeRecord patientUnpayFeeRecord){
        patientUnpayFeeRecord.setUpdateTime(new Date());
        return patientUnpayFeeRecordMapper.updateByPrimaryKeySelective(patientUnpayFeeRecord);
    }

    public PatientUnpayFeeRecord findById(Long id){
        return patientUnpayFeeRecordMapper.selectByPrimaryKey(id);
    }

    public List<PatientUnpayFeeRecord> findByIds(List<Long> ids){
        Example example = new Example(PatientUnpayFeeRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return patientUnpayFeeRecordMapper.selectByExample(example);
    }

    public PatientUnpayFeeRecord findByAccFeeId(String accFeeId){
        Example example = new Example(PatientUnpayFeeRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("unpayFeeId", accFeeId);
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        List<PatientUnpayFeeRecord> feeRecords = patientUnpayFeeRecordMapper.selectByExample(example);
        return CollectionUtils.isEmpty(feeRecords) ? null : feeRecords.get(0);
    }

    public int delById(Long id){
        Example example = new Example(PatientUnpayFeeRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        PatientUnpayFeeRecord patientUnpayFeeRecord = new PatientUnpayFeeRecord();
        patientUnpayFeeRecord.setStatus(CommonStatusEnum.DEL.getCode());
        return patientUnpayFeeRecordMapper.updateByExampleSelective(patientUnpayFeeRecord, example);
    }

}
